package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.entity.HighSearch;
import cn.scxy.bookcity.pojo.entity.Seratch;
import cn.scxy.bookcity.pojo.vo.*;
import cn.scxy.bookcity.service.IGoodsService;
import cn.scxy.bookcity.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户首页的控制器
 * @author zzr
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品模块")
@Slf4j
public class GoodsController {
    @Autowired
    private IGoodsService service;
//    @ApiOperation("查询商品列表")
//    @ApiOperationSupport(order = 300)
//    @GetMapping("/goodsList")
//    public JsonResult<List<GoodsListItemVO>> goodsList(){
//        System.out.println(service.list());
//        List<GoodsListItemVO> list = service.list();
//        return JsonResult.ok(list);
//    }

    @ApiOperation("根据商品id查询商品信息")
    @ApiOperationSupport(order = 300)
    @GetMapping("/ListByGoodId/{id}")
    public JsonResult<GoodsStandardVO> getStandardById(@PathVariable  Long id){
        GoodsStandardVO standardById = service.getStandardById(id);
        log.debug("查询到的结果:{}",standardById);
        return JsonResult.ok(standardById);
    }
    @ApiOperation("")
    @ApiOperationSupport(order = 300)
    @GetMapping("/loadTypeId/{id}")
    public JsonResult<List<GoodsSearchListVO>> loadTypeId(@PathVariable  Long id){
        List<GoodsSearchListVO> GoodsSearchListVO = service.loadTypeId(id);
        log.debug("查询到的结果:{}",GoodsSearchListVO);
        return JsonResult.ok(GoodsSearchListVO);
    }

    @ApiOperation("分组分3页查询")
    @ApiOperationSupport(order = 310)
    @GetMapping("/listLimit3")
    public JsonResult<List<GoodsNameAndUrlVO>> listLimit3(){
        List<GoodsNameAndUrlVO> goodsNameAndUrlVOS = service.listLimit3();
        return JsonResult.ok(goodsNameAndUrlVOS);
    }

    @ApiOperation("查询商品列表的几个")
    @ApiOperationSupport(order = 311)
    @GetMapping("/goodsList")
    public JsonResult<List<List<GoodsListItemVO>>> goodsListByNum(){
        System.out.println(service.listByNum(0));
        List<GoodsListItemVO> list = service.listByNum(0);
        List<GoodsListItemVO> list2 = service.listByNum(8);
        List<List<GoodsListItemVO>> list1=new ArrayList<>();
        list1.add(list);
        list1.add(list2);
        log.debug("{}",list1);
        return JsonResult.ok(list1);
    }
    @ApiOperation("查询推荐商品列表的几个")
    @ApiOperationSupport(order = 300)
    @GetMapping("/selectGoodsListBySort")
    public JsonResult<List<GoodsListItemVO>> selectGoodsListBySort(){
        System.out.println(service.selectGoodsListBySort());
        List<GoodsListItemVO> list = service.selectGoodsListBySort();
        log.debug("{}",list);
        return JsonResult.ok(list);
    }



    @ApiOperation("搜索框搜索结果")
    @ApiOperationSupport(order = 406)
    @PostMapping("/searchResult")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "书名",name = "text",example = "亲子"),
//            @ApiImplicitParam(value = "类别Id",name = "typeid",example = "3"),
//    })
    public JsonResult<List<GoodsSearchListVO>> search(Seratch seratch) throws UnsupportedEncodingException {

//        log.debug("结果为:{}",result);
        String str= URLDecoder.decode(seratch.getText(),"UTF-8");
        System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = " + str);
        seratch.setText(str);
        List<GoodsSearchListVO> result = service.search(seratch);
        System.out.println("seratch = " + seratch);
        log.debug("21132222222222222222222222{}",result);
        return JsonResult.ok(result);
    }

    @ApiOperation("高级搜索")
    @ApiOperationSupport(order = 408)
    @PostMapping("/highSearch")
    /*@ApiImplicitParams({
            @ApiImplicitParam(value = "书名",name = "goodsName",example = "宋词三百年"),
            @ApiImplicitParam(value = "作者",name = "author",example = "艾公子"),
            @ApiImplicitParam(value = "出版社",name = "pressName",example = "科学出版社"),
            @ApiImplicitParam(value = "类别",name = "categoryName",example = "教材"),
            @ApiImplicitParam(value = "包装",name = "packName",example = "精装")
    })*/
    public JsonResult<List<GoodsHighSearchListVO>> highSearch(HighSearch hs) throws UnsupportedEncodingException {
        System.out.println("-----+++++++++++++++++------------- = " + hs);

        String str=null;
        if(hs.getGoodsName()!=null||hs.getGoodsName().isEmpty()) {
             str = URLDecoder.decode(hs.getGoodsName(), "UTF-8");
            hs.setGoodsName(str);
        }else {
            hs.setGoodsName(null);
        }
        System.out.println("--------------------------------------- = " + str);
        String str1=null;
        if(hs.getAuthor()!=null||hs.getAuthor().isEmpty()) {
            str1 = URLDecoder.decode(hs.getAuthor(), "UTF-8");
            hs.setAuthor(str1);
        }else {
            hs.setAuthor(null);
        }
        System.out.println("--------------------------------------- = " + str1);
        String str2=null;
        if(hs.getAuthor()!=null||hs.getAuthor().isEmpty()) {
            str2 = URLDecoder.decode(hs.getPressName(), "UTF-8");
            hs.setPressName(str2);
        }else {
            hs.setPressName(null);
        }
        System.out.println("--------------------------------------- = " + str2);

        System.out.println("--------------------------------------- = " + hs);
        List<GoodsHighSearchListVO> highSearch = service.highSearch(hs);
        System.out.println("--------------------------------------- = " + highSearch);
        return JsonResult.ok(highSearch);
    }

    @ApiOperation("")
    @ApiOperationSupport(order = 300)
    @GetMapping("/searchhot/{hotid}")
    public JsonResult<List<GoodsSearchListVO>> searchhot(@PathVariable  Long hotid){
        List<GoodsSearchListVO> list = service.searchhot(hotid);
        log.debug("查询到的结果:111111111111111111111{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("搜索页面列表展示")
    @ApiOperationSupport(order = 400)
        @GetMapping("/searchList")
    public JsonResult<List<GoodsSearchListVO>> searchList(){
        List<GoodsSearchListVO> list = service.searchList();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过id展示搜索页面列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("/searchListById")
    public JsonResult<List<GoodsSearchListVO>> searchListById(){
        List<GoodsSearchListVO> list = service.searchListById();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过销量展示搜索页面列表")
    @ApiOperationSupport(order = 402)
    @GetMapping("/searchListByNums")
    public JsonResult<List<GoodsSearchListVO>> searchListByNums(){
        List<GoodsSearchListVO> list = service.searchListByNums();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过出版时间展示搜索页面列表")
    @ApiOperationSupport(order = 403)
    @GetMapping("/searchListByHouseTime")
    public JsonResult<List<GoodsSearchListVO>> searchListByHouseTime(){
        List<GoodsSearchListVO> list = service.searchListByHouseTime();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过价格展示搜索页面列表")
    @ApiOperationSupport(order = 404)
    @GetMapping("/searchListByMoney")
    public JsonResult<List<GoodsSearchListVO>> searchListByMoney(){
        List<GoodsSearchListVO> list = service.searchListByMoney();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过评论数量展示搜索页面列表")
    @ApiOperationSupport(order = 405)
    @GetMapping("/searchListByComments")
    public JsonResult<List<GoodsSearchListVO>> searchListByComments(){
        List<GoodsSearchListVO> list = service.searchListByComments();
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过类别id查询")
    @ApiOperationSupport(order = 407)
    @GetMapping("/searchListByCategoryId")
    public JsonResult<List<GoodsSearchListVO>> searchListByCategoryId(Integer categoryId){
        List<GoodsSearchListVO> list = service.searchListByCategoryId(categoryId);
        log.debug("结果为:{}",list);
        return JsonResult.ok(list);
    }



}